package org.road0329.dlt698.axdr;


import org.road0329.dlt698.ReverseByteArrayInputStream;
import org.road0329.dlt698.ReverseByteArrayOutputStream;
import org.road0329.dlt698.asn1.axdr.AxdrEnum;
import org.road0329.dlt698.asn1.axdr.AxdrOctetString;
import org.road0329.dlt698.asn1.axdr.AxdrType;

import java.io.IOException;
import java.nio.ByteBuffer;

public class SubChoiceAppUnit extends AxdrType {
    private ByteBuffer dataCode = null;
    private AppUnitChoice choice = AppUnitChoice._ERR_NONE_SELECTED;
    private AxdrOctetString appUnitPalin = null;
    private AxdrOctetString appUnit = null;

    public SubChoiceAppUnit() {
    }

    public void set_dataCode(ByteBuffer dataCode) {
        this.dataCode = dataCode;
    }

    public void setAppUnit(AxdrOctetString appUnit) {
        resetChoices();
        this.appUnit = appUnit;
        this.choice = AppUnitChoice.APP_UNIT;
    }

    public void setAppUnitPalin(AxdrOctetString appUnitPalin) {
        resetChoices();
        this.appUnitPalin = appUnitPalin;
        this.choice = AppUnitChoice.APP_UNIT_PALIN;
    }

    public int decode(ReverseByteArrayInputStream input) throws Exception {
        int codeLength = 0;
        AxdrEnum choosen = new AxdrEnum();
        choosen.setConst();
        codeLength += choosen.decode(input);
        resetChoices();
        this.choice = AppUnitChoice.getEnumByValue((int)choosen.getValue());
        if (this.choice == AppUnitChoice.APP_UNIT_PALIN) {
            this.appUnitPalin = new AxdrOctetString();
            codeLength += this.appUnitPalin.decode(input);
            return codeLength;
        } else if (this.choice == AppUnitChoice.APP_UNIT) {
            this.appUnit = new AxdrOctetString();
            codeLength += this.appUnit.decode(input);
            return codeLength;
        }
        throw new Exception("Error decoding AxdrChoice: Identifier matched to no item.");
    }

    public int encode(ReverseByteArrayOutputStream output) throws Exception {
        if (this.dataCode != null) {
            for (int i = this.dataCode.capacity() - 1; i >= 0; i--) {
                output.write(this.dataCode.get(i));
            }
            return this.dataCode.capacity();
        }
        if (this.choice == AppUnitChoice._ERR_NONE_SELECTED) {
            throw new Exception("Error encoding AxdrChoice: No item in choice was selected.");
        }
        int codeLength = 0;
        if (this.choice == AppUnitChoice.APP_UNIT) {
            if (this.appUnit != null) {
                codeLength += this.appUnit.encode(output);
                AxdrEnum c = new AxdrEnum();
                c.setMinMaxVal(1);
                codeLength += c.encode(output);
                return codeLength;
            } else {
                throw new Exception("appunit is null");
            }
        } else if (this.choice == AppUnitChoice.APP_UNIT_PALIN) {
            if (this.appUnitPalin != null) {
                codeLength += this.appUnitPalin.encode(output);
                AxdrEnum c = new AxdrEnum();
                c.setMinMaxVal(0);
                codeLength += c.encode(output);
                return codeLength;
            } else {
                throw new Exception("appunitpalin is null");
            }
        }
        throw new Exception("Error encoding AxdrChoice: No item in choice was encoded.");
    }

    public void encodeAndSave(int encodingSizeGuess) throws Exception {
        ReverseByteArrayOutputStream revOStream = new ReverseByteArrayOutputStream();
        revOStream.setBufSize(encodingSizeGuess);
        encode(revOStream);
        this.dataCode = ByteBuffer.wrap(revOStream.getArray());
    }

    public AppUnitChoice getChoiceIndex() {
        return this.choice;
    }

    public void resetChoices() {
        this.choice = AppUnitChoice._ERR_NONE_SELECTED;
        this.appUnitPalin = null;
        this.appUnit = null;
    }

    public String toString() {
        if (this.choice == AppUnitChoice.APP_UNIT_PALIN) {
            if (this.appUnitPalin != null) {
                return "choice: {appUnitPalin: " + this.appUnitPalin + "}";
            } else {
                return "choice is appUnitPalin but appUnitPalin is null";
            }
        } else if (this.choice == AppUnitChoice.APP_UNIT) {
            if (this.appUnit != null) {
                return "choice: {appUnit: " + this.appUnit + "}";
            } else {
                return "choice is appUnit but appUnit is null";
            }
        } else {
            return "unknown";
        }
    }



    enum AppUnitChoice {
        _ERR_NONE_SELECTED(-1),
        APP_UNIT_PALIN(0),
        APP_UNIT(1);

        private final int value;

        AppUnitChoice(int value) {
            this.value = value;
        }

        public int getValue() {
            return value;
        }

        public static AppUnitChoice getEnumByValue(int value) {
            for (AppUnitChoice item : AppUnitChoice.values()) {
                if (value == item.getValue()) {
                    return item;
                }
            }
            return null;
        }
    }
}
